package org.czl.leetcode4;

public class ll {
    public static void main(String[] args) {
        System.out.println(maxScore(new int[]{2,4,8, 16}));
    }
    public static long maxScore(int[] nums) {
        long ans = 0;
        for(int i = 0; i < nums.length; i++){
            int ret = nums[i];
            nums[i] = -1;
            int gcd = findGCD(nums);
            int lcm = findlcm(nums);
            ans = Math.max(ans, gcd * lcm);
            nums[i] = ret;
        }
        int gcd = findGCD(nums);
        int lcm = findlcm(nums);
        ans = Math.max(ans, gcd * lcm);
        return ans;
    }

    static int findlcm(int[] nums){
        int sum = 1;
        for (int i = 0; i < nums.length; i++) {
            if(nums[i] ==  -1){
                continue;
            }else{
                sum *=  nums[i] / gcd(sum, nums[i]);
            }
        }
        return sum;
    }


    static int findGCD(int[] nums){
        int ret = 0;
        if(nums[0] == -1 && nums.length > 1)ret = nums[1];
        else ret = nums[0];
        for(int i = 0; i < nums.length; i++){
            if(i == -1)continue;
            ret = gcd(nums[i], ret);
        }
        return  ret;
    }

    static int gcd(int a, int b) {
        return a % b == 0 ? b : gcd(b, a % b);
    }
}
